package com.xxgc.eow.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @program: ExhibitionOfWorks
 * @description: sa-token对swagger框架的放行
 * @author: Mr.Abel SJY
 * @Email thanksjava@qq.com
 * @create: 2023-05-07 17:08
 **/
@Configuration
public class SaTokenAuthConfigure implements WebMvcConfigurer {
    //放行路径 swagger和登录页面
    private static final String[] EXCLUDE_PATH_PATTERNS = {
            // Swagger
            "/doc.html",
            "**/swagger-ui.html",
            "/swagger-resources/**",
            "/webjars/**",
            "/v2/**",
            "/swagger-ui.html/**",
            "/doc.html/**",
            "/error",
            "/favicon.ico",
            "sso/auth",
            "/csrf"
    };

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOriginPattern("*"); //也可用 setAllowedOrigins(List<String>) 增加多个地址
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        config.setMaxAge(3600L); //缓存时间（秒）

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);

        return new CorsFilter(source);
    }

    // 注册 Sa-Token 拦截器，打开注解式鉴权功能
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，打开注解式鉴权功能
        registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**").excludePathPatterns(EXCLUDE_PATH_PATTERNS);
    }
    //图片路径映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String url = System.getProperty("user.dir")+"\\src\\main\\resources\\markdown\\study.assets\\";
        registry.addResourceHandler("/study.assets/**").addResourceLocations("file:"+url);
    }



}
